package com.spring.pick2pick;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.spring.pick2pick.common.CookieBox;
import com.spring.pick2pick.common.Util;
import com.spring.pick2pick.dao.AdminDao;
import com.spring.pick2pick.model.AdminItem;

@Controller
public class HomeController {
	
	@Autowired
	AdminDao adminDao;
	
	//private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
	
	@RequestMapping(value = {"/", "index"}, method = RequestMethod.GET)
	public String index(Locale locale, Model model) {
		return "index";
	}
	
	@RequestMapping(value = "index", method = RequestMethod.POST)
	public String indexOk(Locale locale, Model model, HttpServletRequest request, HttpServletResponse response) {
		
		String adminId = request.getParameter("adminId");
		String adminPw = request.getParameter("adminPw");
		
		if (adminId == null || adminId.equals("") || adminPw == null || adminPw.equals("")) {
			Util.getInstance().alertPage("아이디나 비밀번호 값이 누락되었습니다.", response);
			return null;
		}
		
		String save_id = request.getParameter("save_id");
		if (save_id == null) {
			CookieBox cbox = new CookieBox(request, response);
			cbox.addCookie("save_id", "", 0);
		} else {
			CookieBox cbox = new CookieBox(request, response);
			cbox.addCookie("save_id", adminId, 60 * 60 * 24);
		}
		
		AdminItem item = new AdminItem();
		item.setAdminId(adminId);
		item.setAdminPw(adminPw);
		AdminItem result = adminDao.doLogin(item);
		
		if (result == null) {
			Util.getInstance().alertPage("로그인 정보가 올바르지 않습니다.", response);
			return null;
		} else {
			HttpSession session = request.getSession();
			session.setMaxInactiveInterval(60 * 120);
			session.setAttribute("ADMIN_INFO", result);
			
			try {
				String html = "<!doctype html>";
				html += "<head>";
				html += "<meta charset='utf-8'>";
				html += "<script>";
				html += "location.replace('index');";
				html += "</script>";
				html += "</head>";
				html += "</html>";
				response.setCharacterEncoding("utf-8");
				PrintWriter out = response.getWriter();
				out.print(html);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		return null;
	}
	
	@RequestMapping(value = "login", method = RequestMethod.GET)
	public String login(Locale locale, Model model, HttpServletRequest request, HttpServletResponse response) {
		
		CookieBox cbox = new CookieBox(request, response);
		String save_id = null;
		if (cbox.exists("save_id")) {
			try {
				save_id = cbox.getValue("save_id");
				model.addAttribute("save_id", save_id);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		return "login";
	}
	
	@RequestMapping(value = "logout", method = RequestMethod.GET)
	public String logout(Locale locale, Model model, HttpServletRequest request, HttpServletResponse response) {
		
		HttpSession session = request.getSession();
		if (session != null) {
			session.invalidate();
		}
		
		try {
			response.sendRedirect("login");
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
	@RequestMapping(value = "download", method = RequestMethod.GET)
	public String download(Locale locale, Model model) {
		return "download";
	}
	
}
